﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Transmute.Graphics
{
    public class RadialGradientBrush : GradientBrush
    {
        #region Properties

        private SpriteBatch spriteBatch;

        public float Precision = 0.006f;

        #endregion

        #region Constructor

        public RadialGradientBrush(int width, int height, Color startColor, Color endColor) : base(GraphicsHelper.GraphicsDevice, width, height, startColor, endColor, new Vector2(width / 2, height / 2), new Vector2(width, height))
        {
            spriteBatch = GraphicsHelper.SpriteBatch;
            Render();
        }

        public RadialGradientBrush(int width, int height, Vector2 centerPoint, Color startColor, Color endColor) : base(GraphicsHelper.GraphicsDevice, width, height, startColor, endColor, centerPoint, new Vector2((centerPoint.X != 0 ? 0 : width), (centerPoint.Y != 0 ? 0 : height)))
        {
            spriteBatch = GraphicsHelper.SpriteBatch;
            Render();
        }

        #endregion

        #region Methods

        public override void Render()
        {
            GraphicsDevice.SetRenderTarget(this);
            GraphicsDevice.Clear(Color.Transparent);

            var r = GraphicsHelper.GetDistance(StartPoint, EndPoint);

            spriteBatch.Begin();
                for (float h = 0f; h < (float)r; h += 1f)
                {
                    Color color = Color.Lerp(StartColor, EndColor, (float)((h / GraphicsHelper.GetDistance(StartPoint, EndPoint))));
                    GraphicsHelper.DrawCircle((int)StartPoint.X, (int)StartPoint.Y, h, 2, color);
                }
            spriteBatch.End();

            GraphicsDevice.SetRenderTarget(null);
        }

        #endregion
    }
}
